package com.zhoug.zmui3.input;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filterable;
import android.widget.ListAdapter;

import com.zhoug.zmui3.autocomplete.OnItemClickListener;
import com.zhoug.zmui3.autocomplete.ZMUIMultiAutoCompleteTextView;

import androidx.annotation.Nullable;

/**
 * 输入自动补全提示,支持多选
 * @Author: zhoug
 * @Date: 2024-07-22
 * @Description:
 */
public class ZMUIInputMultiAutoCompleteTextView extends ZMUIInputTextView {
    public ZMUIInputMultiAutoCompleteTextView(Context context) {
        super(context);
    }

    public ZMUIInputMultiAutoCompleteTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ZMUIInputMultiAutoCompleteTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected View createValueView(ViewGroup parent) {
        if (mValueView == null) {
            mValueView = new ZMUIMultiAutoCompleteTextView(getContext());
            //去掉默认背景
            mValueView.setBackground(null);
            //去掉默认padding
            mValueView.setPadding(0, 0, 0, 0);
        }
        return mValueView;
    }


    @Override
    public ZMUIMultiAutoCompleteTextView getValueView() {
        return (ZMUIMultiAutoCompleteTextView) mValueView;
    }



    /**
     * 禁止输入但可以点击
     *
     * @param enabled
     */
    public void setInputEnabled(boolean enabled) {
        mValueView.setCursorVisible(enabled);
        mValueView.setFocusable(enabled);
        mValueView.setFocusableInTouchMode(enabled);
    }

    /**
     * 设置输入框点击监听
     *
     * @param onClickListener
     */
    public void setInputViewOnClickListener(View.OnClickListener onClickListener) {
        setInputEnabled(false);
        mValueView.setClickable(true);
        mValueView.setOnClickListener(onClickListener);
    }


    public <T extends ListAdapter & Filterable> void setAdapter(T adapter) {
        getValueView().setAdapter(adapter);
    }

    public ListAdapter getAdapter() {
        return getValueView().getAdapter();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        getValueView().setOnItemClickListener(onItemClickListener);
    }

    /**
     * 默认禁用过滤自动补全
     *
     * @param value
     */
    public void setValue(String value) {
        getValueView().setText(value, false);
    }

    public void setValue(String value, boolean filter) {
        getValueView().setText(value, filter);
    }


    @Nullable
    @Override
    public String getValue() {
        String value = super.getValue();
        //去掉两头的逗号
        if (value != null) {
            if (value.startsWith(",")) {
                value = value.substring(1);
            }
            if (value.endsWith(",")) {
                value = value.substring(0, value.length() - 1);
            }
        }
        return value;
    }

    @Override
    public void clearFocus() {
        super.clearFocus();
        mValueView.clearFocus();
    }

    @Override
    protected void onClearClick(View view) {
//        super.onClearClick(view);
        setValue("", false);
    }


}


